package com.dshore.messagecenter.util;

import java.util.List;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dshore.messagecenter.model.msgrecord.ReceiveMsg;

@Component
public class RabbitPublish {

	@Autowired
	private AmqpTemplate amqpTemplate;
	
	/**
	 * 
	 * 添加单个主题队列配置
	 * 必须在更新数据库后调用
	 * @param topicQueueNo 主题队列编号
	 * @param queueNames 监控队列名称
	 */
	void addTopicQueue(String topicQueueNo, String queueNames) {
		amqpTemplate.convertAndSend("amq.fanout", "", "addTopicQueue:" + topicQueueNo);
		amqpTemplate.convertAndSend("amq.fanout", "", "addTargetApp:" + queueNames);
	}
	
	/**
	 * 添加一组目标应用
	 * 必须在更新数据库后调用
	 * @param queueNames 监控队列名称
	 */
	void addTargetApp(String queueNames) {
		amqpTemplate.convertAndSend("amq.fanout", "", "addTargetApp:" + queueNames);
	}
	
	/**
	 * 删除单个主题队列配置
	 * 必须在更新数据库后调用
	 * @param topicQueueNo 主题队列编号
	 * @param queueNames 监控队列名称
	 */
	void removeTopicQueue(String topicQueueNo, String queueNames) {
		amqpTemplate.convertAndSend("amq.fanout", "", "removeTopicQueue:" + topicQueueNo);
		amqpTemplate.convertAndSend("amq.fanout", "", "removeTargetApp:" + queueNames);
	}
	
	/**
	 * 删除一组目标应用
	 * 必须在更新数据库后调用
	 * @param queueNames 监控队列名称
	 */
	void removeTargetApp(String queueNames) {
		amqpTemplate.convertAndSend("amq.fanout", "", "removeTargetApp:" + queueNames);
	}
	
	/**
	 * 消息重新发送
	 * @param queueName
	 * @param receiveMsgs
	 */
	void msgResend(String queueName, List<ReceiveMsg> receiveMsgs) {
		// 循环发送
		for (ReceiveMsg receiveMsg : receiveMsgs) {
			amqpTemplate.convertAndSend(queueName, JSON.toJSONString(receiveMsg));
		}
	}
}
